package com.southwind.controller;

import com.southwind.entity.Menu;
import com.southwind.entity.MenuVo;
import com.southwind.entity.Type;
import com.southwind.repository.MenuRepository;
import com.southwind.repository.TypeRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("/menu")
public class MenuHandler {
    @Value("${server.port}")
    private String port;

    @Resource
    private MenuRepository menuRepository;

    @Resource
    private TypeRepository typeRepository;

    @GetMapping("/index")
    public String index(){
        return "当前menu的端口为"+this.port;
    }

    @GetMapping("/findAll/{index}/{limit}")
    public MenuVo findAll(@PathVariable("index") Integer index,@PathVariable("limit") Integer limit){
        return new MenuVo(0,"",menuRepository.count(),menuRepository.findAll(index,limit));
    }
//    根据id删除指定的菜品
    @DeleteMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id){
        menuRepository.deleteById(id);
    }
//    查询全部的口味
    @GetMapping("/findTypes")
    public List<Type> findTypes(){
      return  typeRepository.findTypes();
    }
//    添加菜品
    @PostMapping("/save")
    public void save(@RequestBody Menu menu){
        System.out.println("-----------------");
        System.out.println(menu);
        menuRepository.save(menu);
    }

//    根据id查找对应的数据
    @GetMapping("/findById/{id}")
    public Menu findById(@PathVariable("id") Integer id){
      return  menuRepository.findById(id);
    }

//    更新数据
    @PutMapping("/update")
    public void update(@RequestBody Menu menu){
        menuRepository.update(menu);
    }

}
